package com.ccp.dev.workflow.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.model.TaskOpinion;
import com.ccp.dev.workflow.service.ProcessRunService;
import com.ccp.dev.workflow.service.TaskOpinionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassDescribe:流程任务审批意见控制器类
 * @author :jiayueqiao
 * Date: 2019-08-13
 * Since:1
 */
@Controller
@RequestMapping(UrlConstants.PROJECT_ROOT +UrlConstants.PROJECT_MODULE_WORKFLOW+"/taskOpinion")
@Action(ownermodel=SysAuditModelType.PROCESS_CENTER)
public class TaskOpinionController extends SysBaseController {

    @Resource
    private TaskOpinionService taskOpinionService;
    @Resource
    private ProcessRunService processRunService;

    /**
     * 流程审批历史页面
     * @param request request
     * @param model 视图模型
     * @return 页面地址
     */
    @RequestMapping(UrlConstants.VIEW_DISPATCH_PREFIX+UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "流程审批历史页面")
    public String viewList(HttpServletRequest request, Model model) {
        String runId = RequestUtil.getString(request,"runId");
        if(StringUtil.isNotEmpty(runId)){
            ProcessRun processRun=processRunService.getById(runId);
            if(null!=processRun){
                model.addAttribute("processRun",processRun);
            }
        }
        return getAutoView();
    }

    /**
     * 流程审批历史数据
     * @param request request
     * @return ResultData
     */
    @RequestMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description = "查询流程审批历史数据",detail = "查询流程审批历史数据<#if fl>成功<#else>失败</#if>")
    @ResponseBody
    public ResultData list(HttpServletRequest request) {
        try{
            String runId = RequestUtil.getString(request,"runId");
            if(StringUtil.isEmpty(runId)){
                throw new Exception();
            }
            //取得关联的流程实例ID
            List<TaskOpinion> list=taskOpinionService.getByRunId(runId);
            //设置执行人
            list = taskOpinionService.setTaskOpinionExecutor(list);
            SysAuditThreadLocalHolder.putParamerter("fl", true);
            return getResultDataSuccess("operation.success",list);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("fl", false);
            return getResultDataError("operation.failure",1,e.getMessage());
        }
    }
}
